package com.vframework.rest.filter;

import java.lang.reflect.Method;

import javax.annotation.security.DenyAll;
import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.container.ResourceInfo;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.ext.Provider;

import org.springframework.beans.factory.annotation.Autowired;

import com.util.StringUtil;
import com.util.memcached.CacheUtil;
import com.vframework.rest.bean.SessionCache;
import com.vframework.rest.common.RestResponse;
import com.vframework.rest.util.EopConstant;

/***************************************************************
 * Name:        JerseyAuthenticationFilter.java
 * Create At:   2017年8月9日
 * Module:      
 * Description: [功能描述]
 * @author      shanzhitong
 * ------------------------------------------------------------
 * 修改历史
 * 序号         日期                       修改人               修改原因
 * 1    2017年8月9日      shanzhitong    
 * 2
***************************************************************/
@Provider
public class JerseyAuthenticationFilter implements ContainerRequestFilter {

	@Context
	private ResourceInfo resourceInfo;

	@Context
	private HttpServletRequest httpRequest;

	@Context
    private UriInfo uriInfo;
	
	@Context
	private HttpServletResponse httpResponse;
	
	@Autowired
    private CacheUtil cacheUtil;
	
	public static final String AUTHORIZATION = "Authorization";
	public static final String ORGID = "orgId";
	
	public void filter(ContainerRequestContext requestContext) {
        String method = httpRequest.getMethod();
        if ("options".equalsIgnoreCase(method)) {
            return;
        }
        //获取token查memcache
        String token = requestContext.getHeaderString(AUTHORIZATION);
//        String orgId = requestContext.getHeaderString(ORGID);
//        if (!StringUtil.isEmpty(orgId))
//            httpRequest.getSession().setAttribute(EopConstant.CACHE_SESSION_KEY, orgId);
        SessionCache sessionCache = cacheUtil.get(EopConstant.CACHE_TOKEN_KEY + token);
        cacheUtil.set(EopConstant.CACHE_TOKEN_KEY + token, sessionCache, 60 * 24 * 7);
        /**
         * 三级权限
         * PermitAll  这个不受任何限制
         * 无  token合法可以访问
         * DenyAll  注册登录后可以访问
         **/
        Method resourceMethod = resourceInfo.getResourceMethod();
        if (resourceMethod.isAnnotationPresent(PermitAll.class)) {//无任何限制
            return;
        } else if (resourceMethod.isAnnotationPresent(DenyAll.class)) {//需要登录访问的请求拦截
            if (sessionCache == null || sessionCache.getMember() == null) {
                requestContext.abortWith(Response.status(Response.Status.OK).entity(new RestResponse(RestResponse.Status.UNLOGIN)).build());
                return;
            }
        } else {//token合法可以访问
            if (null == sessionCache) {//token非法
                requestContext.abortWith(Response.status(Response.Status.OK).entity(new RestResponse(RestResponse.Status.UNAUTHORIZED)).build());
                return;
            }
        }
	}
}